package com.ruoyi.patrol.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.PatrolConstans;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.patrol.domain.PtStaff;
import com.ruoyi.patrol.service.IPtStaffService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 行政人员Controller
 *
 * @author lkm
 * @date 2022-11-14
 */
@RestController
@RequestMapping("/patrol/staff")
public class PtStaffController extends BaseController {
    @Autowired
    private IPtStaffService ptStaffService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IPtStaffService staffService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询行政人员列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:staff:list')")
    @GetMapping("/list")
    public TableDataInfo list(PtStaff ptStaff) {
        if (ptStaff.getSchoolId() == null) {
            ptStaff.setSchoolId(SecurityUtils.getSchoolId());
        }
        startPage();
        List<PtStaff> list = ptStaffService.selectPtStaffList(ptStaff);
        return getDataTable(list);
    }

    /**
     * 导出行政人员列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:staff:export')")
    @Log(title = "行政人员", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PtStaff ptStaff) {
        List<PtStaff> list = ptStaffService.selectPtStaffList(ptStaff);
        ExcelUtil<PtStaff> util = new ExcelUtil<PtStaff>(PtStaff.class);
        return util.exportExcel(list, "staff");
    }

    /**
     * 导入行政人员列表
     */
    @Log(title = "行政人员", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('patrol:staff:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<PtStaff> util = new ExcelUtil<PtStaff>(PtStaff.class);
        List<PtStaff> staffList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = staffService.importStaff(staffList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入行政信息模板
     *
     * @return
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<PtStaff> util = new ExcelUtil<PtStaff>(PtStaff.class);
        return util.importTemplateExcel("行政人员信息模板");
    }

    /**
     * 获取行政人员详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:staff:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ptStaffService.selectPtStaffById(id));
    }

    /**
     * 获取行政人员详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:staff:query')")
    @GetMapping(value = "/count")
    public AjaxResult getStaffCount() {
        AjaxResult result = AjaxResult.success();
        Long schoolId = SecurityUtils.getSchoolId();
        result.put("staffCount", ptStaffService.selectStaffCount(schoolId));
        return result;
    }

    /**
     * 新增行政人员
     */
    @PreAuthorize("@ss.hasPermi('patrol:staff:add')")
    @Log(title = "行政人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtStaff ptStaff) {
        if (UserConstants.NOT_UNIQUE.equals(ptStaffService.checkPhoneUnique(ptStaff))) {
            return AjaxResult.error("新增行政人员'" + ptStaff.getStaffName() + "'失败，手机号码已存在");
        }
        if (PatrolConstans.NOT_UNIQUE.equals(ptStaffService.checkIdcardUnique(ptStaff))) {
            return AjaxResult.error("新增行政人员'" + ptStaff.getStaffName() + "'失败，身份证号码已存在");
        }
        if (PatrolConstans.NOT_UNIQUE.equals(sysUserService.checkUserNameUnique(ptStaff.getStaffPhonenumber()))) {
            return AjaxResult.error("新增行政人员'" + ptStaff.getStaffName() + "'失败，手机号码已存在");
        }

        return toAjax(ptStaffService.insertPtStaff(ptStaff));
    }

    /**
     * 修改行政人员
     */
    @PreAuthorize("@ss.hasPermi('patrol:staff:edit')")
    @Log(title = "行政人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtStaff ptStaff) {
        if (UserConstants.NOT_UNIQUE.equals(ptStaffService.checkPhoneUnique(ptStaff))) {
            return AjaxResult.error("修改行政人员'" + ptStaff.getStaffName() + "'失败，手机号码已存在");
        }
        if (PatrolConstans.NOT_UNIQUE.equals(ptStaffService.checkIdcardUnique(ptStaff))) {
            return AjaxResult.error("修改行政人员'" + ptStaff.getStaffName() + "'失败，身份证号码已存在");
        }
        return toAjax(ptStaffService.updatePtStaff(ptStaff));
    }

    /**
     * 删除行政人员
     */
    @PreAuthorize("@ss.hasPermi('patrol:staff:remove')")
    @Log(title = "行政人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        if (ptStaffService.isStaffOnSchedule(ids[0])) {
            return AjaxResult.error("该人员有未完成的巡查，暂不能删除！");
        }
        return toAjax(ptStaffService.deletePtStaffByIds(ids));
    }

    @GetMapping("/optionselect")
    public AjaxResult optionselect(PtStaff ptStaff) {
        ptStaff.setSchoolId(SecurityUtils.getSchoolId());
        List<PtStaff> list = ptStaffService.selectPtStaffList(ptStaff);
        return AjaxResult.success(list);
    }
}
